﻿#region Using Region
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICE.Vacations.DataBase;
using Unisys.InfoImage.IIFCAL.IIFWorkflow;
using System.Data.SqlClient;
using ICE.Vacations.Helper;
using ICE.Vacations.Helper.Services;
using ICE.Vacations.InfoImage;

#region Logger References
using ICE.Vacations.Helper.Services;
#endregion
#region PDF References

using iTextSharp.text.html.simpleparser;
using iTextSharp.text;
using iTextSharp.text.pdf;

#endregion


#endregion

namespace ICE.Vacations.Request
{
    public class Documents
    {

        public OracleDataClass oracle;
        public SQLDataClass sql = new SQLDataClass();
        public IIFWorkItem worktiem = new IIFWorkItem();

        private List<Documents> documents;
        public List<Documents> GetDocuments()
        {
            return documents;
        }

        #region Global Resources

        #region BD RECURSOS


        public string username { get; set; }
        public string employeeID { get; set; }
        public string apellido1 { get; set; }
        public string apellido2 { get; set; }
        public string employeeName { get; set; }
        public string eMail { get; set; }
        public string cod_cf { get; set; }
        public string cod_direccion { get; set; }
        public string tipo { get; set; }
        public string aprobador1 { get; set; }
        public string aprobador2 { get; set; }
        public string aprobador3 { get; set; }

        #endregion

        #region Form View

        public string managerID { get; set; }
        public string docUnit { get; set; }
        public string date { get; set; }
        public string year { get; set; }
        public string revision { get; set; }
        public string dateFrom { get; set; }
        public string dateTo { get; set; }
        public string dateRegistered { get; set; }
        public string dateSent { get; set; }
        public int totalDays { get; set; }
        public int totalRequestedDays { get; set; }
        public string requestID { get; set; }
        public string bossID { get; set; }
        public string state { get; set; }

        #endregion

        #region IIF

        #endregion


        #endregion
        public void SelectAll()
        {
            sql = new SQLDataClass();
            Services.sendLog("Buscando solicitudes correspondientes al jefe: 114810283", Services.errorType.InformationMessage);
            SqlDataReader reader = sql.SearchByBoss("114810283");
            //Services.sendLog("tiene filas:" + reader.HasRows, Services.errorType.InformationMessage);

            documents = new List<Documents>();

            try
            {
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Documents doc = new Documents();
                        doc.requestID = reader.GetValue(reader.GetOrdinal("IDSOLICITUD")).ToString();
                        doc.username = reader.GetValue(reader.GetOrdinal("USUARIO")).ToString();
                        doc.employeeID = reader.GetValue(reader.GetOrdinal("EMPLEADO")).ToString();
                        doc.totalDays = Convert.ToInt32(reader.GetValue(reader.GetOrdinal("TDIASDISPONIBLES")).ToString());
                        doc.totalRequestedDays = Convert.ToInt32(reader.GetValue(reader.GetOrdinal("TDIASSOLICITADOS")).ToString());
                        doc.dateFrom = Convert.ToDateTime(reader.GetValue(reader.GetOrdinal("FECHA_INICIO")).ToString()).ToString("MM/dd/yyyy");
                        doc.dateTo = Convert.ToDateTime(reader.GetValue(reader.GetOrdinal("FECHA_FIN")).ToString()).ToString("MM/dd/yyyy");
                        doc.dateRegistered = Convert.ToDateTime(reader.GetValue(reader.GetOrdinal("FECHA_REGISTRO")).ToString()).ToString("MM/dd/yyyy");
                        doc.dateSent = reader.GetValue(reader.GetOrdinal("FECHA_ENVIO_WF")).ToString();
                        doc.aprobador1 = reader.GetValue(reader.GetOrdinal("APROBADOR1")).ToString();
                        doc.aprobador2 = reader.GetValue(reader.GetOrdinal("APROBADOR2")).ToString();
                        doc.aprobador3 = reader.GetValue(reader.GetOrdinal("APROBADOR3")).ToString();
                        documents.Add(doc);
                    }
                }

            }
            catch (Exception exc)
            {
                Services.sendLog("Ocurrio un error: " + exc.Message, Services.errorType.DBError);
                //return null;
            }
            // return documents;
        }

        public Documents convertRequestObject(SqlDataReader reader)
        {
            Documents document = new Documents();
            while (reader.Read())
            {
                try
                {
                    document.username = reader.GetSqlValue(0).ToString();
                    document.employeeID = reader.GetSqlValue(1).ToString();
                    document.apellido1 = reader.GetSqlValue(2).ToString();
                    document.apellido2 = reader.GetSqlValue(3).ToString();
                    document.employeeName = reader.GetSqlValue(4).ToString();
                    document.eMail = reader.GetSqlValue(5).ToString();

                    document.tipo = reader.GetSqlValue(8).ToString();
                    document.aprobador1 = reader.GetSqlValue(9).ToString();
                    document.aprobador2 = reader.GetSqlValue(10).ToString();
                    document.aprobador3 = reader.GetSqlValue(11).ToString();

                }
                catch (Exception e)
                {

                }

            }
            return document;
        }

        public void CreateRequest(Request doc)
        {
            try
            {


                Services.sendLog("Creando solicitud [" + requestID + "]...", Services.errorType.InformationMessage);
                try
                {
                    if (sql.SQLInsertSolicitud(doc.requestID, doc.username, doc.employeeID, 1 /*doc.totalDays*/, 1 /*doc.totalSolicitados*/, doc.dateFrom, doc.dateTo, DateTime.Now.ToString("dd/MM/yyyy"), DateTime.Now.ToString("dd/MM/yyyy"), doc.state, doc.aprobador1, doc.aprobador2, doc.aprobador3, true))
                    {
                        Services.sendLog("Solicitud [" + requestID + "] creada satisfactoriamente en BD..", Services.errorType.InformationMessage);

                        try
                        {
                            Services.sendLog("Iniciando la generación del PDF...", Services.errorType.InformationMessage);
                            doc.SavePDF(doc, ConfigApp.pdfPath);
                        }
                        catch (Exception ex)
                        {
                            Services.sendLog("Ha sucedido un error al momento de generar el PDF. Error: [" + ex.Message + "].", Services.errorType.ConfigurationError);
                        }


                        if (worktiem.CreateIIFClient(ConfigApp.IIFUser, ConfigApp.IIFPassword, ConfigApp.IIFDomain))
                        {
                            Services.sendLog("Creando workitem de la solicitud [" + requestID + "]..", Services.errorType.InformationMessage);

                            if (worktiem.CreateWorkitem(doc.requestID, doc.docUnit, doc.employeeID, doc.employeeName, "folio", "tipoo", doc.cod_cf, doc.date, doc.year, doc.revision, doc.dateFrom, doc.dateTo, "comments", doc.state))
                            {

                            }
                        }
                        else
                        {
                            Services.sendLog("No se logró la conexión con InfoImage al servidor [" + ConfigApp.IIFDomain + "] con el usuario [" + ConfigApp.IIFUser + "]", Services.errorType.IIFError);
                        }

                    }


                }
                catch (Exception e)
                {
                    Services.sendLog("Ocurrió un error al intentar crear la solicitud. Error: " + e.Message, Services.errorType.DBError);
                }
            }
            catch (Exception exc)
            {
            }

        }

        public void SaveRequest(Documents doc)
        {
            try
            {
                Services.sendLog("Guardando cambios de solicitud [" + requestID + "]...", Services.errorType.InformationMessage);

            }
            catch (Exception exc)
            {
            }
        }

        public void CheckRequest(Documents doc)
        {
        }

        public void SearchRequest(Documents doc)
        {

        }

        public void CopyPDF()
        {
            throw new System.NotImplementedException();
        }
    }
}
